package com.secsbrain.cmdb.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.secsbrain.cmdb.biz.IIpService;
import com.secsbrain.cmdb.dal.model.Ip;
import com.secsbrain.cmdb.web.dto.IpTest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.common.ResponseResult;

/**
 * IP控制层
 * @author zhaoyi
 *
 */
@RestController
@RequestMapping("api/ip")
public class IpController{
	
    private static final Logger logger = LoggerFactory.getLogger(IpController.class);
	
	@Value("${cmdb.offline.agent.url:}")
	private String offlineAgentUrl;
	
	@Value("${cmdb.online.agent.url:}")
	private String onlineAgentUrl;
	
    @Autowired
    private IIpService ipService;
    
	public static final String[] ONLINE_IP_ARRAY={"10.0.100.","10.0.101.","10.0.102.","172.16.188.","172.16.189."};
	
	public static final String[] OFFLINE_IP_ARRAY={"10.0.0.","10.0.1.","10.0.11."};
    
    /**
   	 * @api {GET} /api/ip/list  查询IP列表
   	 * 
   	 * @apiGroup IP
   	 * 
   	 * @apiParam {Number}  [istatus]  	状态
   	 * @apiParam {String}  [ip]         IP地址
   	 * @apiParam {String}  [ipSegment]  IP段
   	 * 
   	 * @apiSuccess {Object[]} data    	数据列表
   	 * @apiSuccess {Number} data.id     	id
   	 * @apiSuccess {String} data.ip   		IP地址
   	 * @apiSuccess {Number} data.istatus    状态 0-占用 ，1-可用
   	 * @apiSuccess {String} data.updatorName  更新人
   	 * @apiSuccess {Date} data.updateTime     更新时间
   	 * 
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseResult<?> queryList(Integer page, Integer limit, Ip params) {
    	Page<Ip> pageBean=new Page<Ip>(page, limit);
    	Page<Ip> result=ipService.queryIpPageList(pageBean, params);
    	return new ResponseResult(result);
    }
    
    /**
   	 * @api {GET} /api/ip/ipSegment  查询IP段
   	 * 
   	 * @apiGroup IP
   	 * 
   	 * @apiSuccess {String[]} data    	数据列表
   	 * 
     */
    @RequestMapping(value = "/ipSegment", method = RequestMethod.GET)
    public ResponseResult<?> ipSegment() {
    	List<String> ipSegmentList=new ArrayList<String>();
    	for(String ip:ONLINE_IP_ARRAY){
    		ipSegmentList.add(ip+"0");
    	}
    	for(String ip:OFFLINE_IP_ARRAY){
    		ipSegmentList.add(ip+"0");
    	}
    	return new ResponseResult(ipSegmentList);
    }
    
    /**
   	 * @api {GET} /api/ip/checkAll  扫描全部IP
   	 * 
   	 * @apiGroup IP
   	 * 
     */
    @RequestMapping(value = "/checkAll", method = RequestMethod.GET)
    public ResponseResult<?> checkAll() {
    	if(StringUtils.isNotEmpty(offlineAgentUrl)){
    		String hostRes = new RestTemplate().getForObject(offlineAgentUrl + "/ip/testAll", String.class);
    		logger.debug(hostRes);
    		List<IpTest> list = JSON.parseArray(hostRes, IpTest.class);
    		ipService.updateIp(list);
    	}
    	
    	if(StringUtils.isNotEmpty(onlineAgentUrl)){
    		String hostRes = new RestTemplate().getForObject(onlineAgentUrl + "/ip/testAll", String.class);
    		List<IpTest> list = JSON.parseArray(hostRes, IpTest.class);
    		ipService.updateIp(list);
    	}
    	
    	return new ResponseResult();
    }
    
    /**
   	 * @api {GET} /api/ip/check  扫描单个IP
   	 * 
   	 * @apiParam {String}  [ip]         IP地址
   	 * @apiGroup IP
   	 * 
     */
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    public ResponseResult<?> check(String ip) {
        Assert.hasText(ip, "ip不能为空");
        //10.0.11.6
        String ipSegment=ip.substring(0, ip.lastIndexOf(".")+1);
        String requestUrl=this.isOnlineIp(ipSegment)?onlineAgentUrl:offlineAgentUrl;
    	String hostRes = new RestTemplate().getForObject(requestUrl + "/ip/testSingle?ip="+ip, String.class);
    	IpTest ipTest = JSON.parseObject(hostRes, IpTest.class);
    	List<IpTest> list = new ArrayList<IpTest>();
    	list.add(ipTest);
		ipService.updateIp(list);
    	return new ResponseResult();
    }
    
    private boolean isOnlineIp(String ipSegment){
    	boolean flag=false;
    	for(String ip:ONLINE_IP_ARRAY){
    		if(ip.equals(ipSegment)){
    			flag=true;
    			break;
    		}
    	}
    	return flag;
    }
    
}
